<?php
namespace Home\Controller;
use Think\Controller;
class ApiController extends Controller {
	/**
	* 省
	*/
	public function getProvince(){
		$list = S('S_provice');
		if(!$list){			
			$model = M('Provice');
			$field = array('provice_id as id','provice_name as name','letter');
			$list = $model->field($field)->order('letter ASC,id desc')->select();
			S('S_provice',$list,1296000);
		}
		$_list = [];
		foreach($list as $k => $v){
			if(preg_match("/^[A-G]$/i",$v['letter'],$rst)){
				$_list['A-G'][] = $v;
			}elseif(preg_match("/^[H-K]$/i",$v['letter'],$rst)){
				$_list['H-K'][] = $v;
			}elseif(preg_match("/^[L-S]$/i",$v['letter'],$rst)){
				$_list['L-S'][] = $v;
			}elseif(preg_match("/^[T-Z]$/i",$v['letter'],$rst)){
				$_list['T-Z'][] = $v;
			}
		}
		unset($list);
		$list = $_list;
		if($list){
			exit(rst(0,array('list' => $list),'获取省地址成功'));
		}else{
			exit(rst(1,'','获取数据失败'));
		}
	}
	/**
	* 城市
	*/
	public function getCity(){
		$pid = I('id');
		$list = S('S_city_'.$pid);
		if(!$list){
			$model = M('City');
			$field = array('province_id','city_name as name','city_id as id');
			$list = $model->field($field)->where(array('province_id' => $pid))->order('id ASC')->select();
			S('S_city_'.$pid,$list,1296000);
		}
		if(IS_POST){
			$_list = [];
			foreach($list as $k => $v){
				if($v['province_id'] == $pid){
					$_list[] = $v;
				}
			}
			unset($list);
			$list = $_list;
			if($list){
				exit(rst(0,array('list' => $list),'获取城市地址成功'));
			}else{
				exit(rst(1,'','获取数据失败'));
			}
		}else{
			exit(rst(1,'','获取数据失败'));
		}
	}
	/**
	* 县区
	*/
	public function getCounty(){
		$pid = I('id');
		$list = S('S_county_'.$pid);
		if(!$list){
			$model = M('County');
			$field = array('city_id','county_name as name','county_id as id');
			$list = $model->field($field)->where(array('city_id' => $pid))->order('id ASC')->select();
			S('S_county_'.$pid,$list,1296000);
		}
		if(IS_POST){
			$_list = [];
			foreach($list as $k => $v){
				if($v['city_id'] == $pid){
					$_list[] = $v;
				}
			}
			unset($list);
			$list = $_list;
			if($list){
				exit(rst(0,array('list' => $list),'获取县区地址成功'));
			}else{
				exit(rst(1,'','获取数据失败'));
			}
		}else{
			exit(rst(1,'','获取数据失败'));
		}
	}
	/**
	* 街镇
	*/
	public function getTown(){
		$pid = I('id');
		$list = S('S_Town_'.$pid);
		if(!$list){
			$model = M('Town');
			$field = array('county_id','town_name as name','town_id as id');
			$list = $model->field($field)->where(array('county_id' => $pid))->order('id ASC')->select();
			S('S_Town_'.$pid,$list,1296000);
		}
		if(IS_POST){
			$_list = [];
			foreach($list as $k => $v){
				if($v['county_id'] == $pid){
					$_list[] = $v;
				}
			}
			unset($list);
			$list = $_list;
			if($list){
				exit(rst(0,array('list' => $list),'获取街镇地址成功'));
			}else{
				exit(rst(1,'','获取数据失败'));
			}
		}else{
			exit(rst(1,'','获取数据失败'));
		}
	}
	/**
	* 社区、村
	*/
	public function getVillage(){		
		$pid = I('id');
		$list = S('S_Village_'.$pid);
		if(!$list){
			$model = M('Village');
			$field = array('town_id','village_name as name','village_id as id');
			$list = $model->field($field)->where(array('town_id' => $pid))->order('id ASC')->select();
			if($list){
				S('S_Village_'.$pid,$list,1296000);
			}else{
				exit(rst(1,'','获取数据失败'));
			}
		}
		if(IS_POST){
			$_list = [];
			foreach($list as $k => $v){
				if($v['town_id'] == $pid){
					$_list[] = $v;
				}
			}
			unset($list);
			$list = $_list;
			if($list){
				exit(rst(0,array('list' => $list),'获取社区、村地址成功'));
			}else{
				exit(rst(1,'','获取数据失败'));
			}
		}else{
			exit(rst(1,'','获取数据失败'));
		}
	}
}